import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../ActionManager/ActionArgHelper";
 

/*
获取OPS
*/
export class State_GetOPS<T extends IStateArg<PowerCreep>>  extends State<T>
{

    public  getActions(arg:T):Action[]
    {
        if(!arg.entity.memory.taskid)
        {
            return []
        }
        const toptask =  arg.entity.ownerRoom().findTaskById<ITaskDeclare['pc']>(arg.entity.memory.taskid);
        
        if(toptask)
        {
            const powerinfo:{
                className: PowerClassConstant;
                level: number[];
                cooldown: number;
                effect?: number[];
                range?: number;
                ops?: number | number[];
                duration?: number | number[];
            } = POWER_INFO[toptask.data.arg.POWER];
            if(powerinfo.ops )
            {
                if(!toptask.data.OPSstoreId)
                {
                    
                    const coreentity = arg.entity.ownerRoom().getEntity("CoreEntity");
                    toptask.data.OPSstoreId = coreentity.getTakeStores ({resourceType:RESOURCE_OPS,amount:100})[0]?.id;
                    return [];
                }
                if(typeof powerinfo.ops == "number")
                {
                    const store = Game.getObjectById(toptask.data.OPSstoreId);
                    if(store)
                    {
                        const opsamount = store.store.getUsedCapacity(RESOURCE_OPS);
                        if(opsamount && opsamount>=powerinfo.ops)
                        {
                            // 取资源
                            if(arg.entity.pos.distance(store.pos)>1)
                            {
                                // const motoarg:IMoveArg={creepid:"" as Id<Creep>,pos:store.pos,range:1,opt:{}};
                                // arg.entity.moveTo(motoarg);
                                return [ActionArgHelper.actionMoveto(arg.entity,{pos: store.pos,range:1},{})]
                            }
                            else
                            {
                                if(store instanceof Creep)
                                {
                                    store.transfer( arg.entity,RESOURCE_OPS,powerinfo.ops );
                                }
                                else
                                {
                                    arg.entity.withdraw(store,RESOURCE_OPS,powerinfo.ops );
                                }
                            }
                        }
                        else
                        {
                            // 发布资源缺少清单
                        }
                    }
                    else
                    {
                        // 没有设置OPS仓库,则自己找
                    }
                }
                else
                {
                    arg.entity.log("warn",`task:${JSON.stringify(toptask)} OPS 需求数量不正确  OPS:${ JSON.stringify(powerinfo.ops)}`);
                }
            }
        }
        return [];
    }

}
